<article class="module width_full">
    <header> <h3 class="tabs_involved">DANH MỤC NHÀ XUẤT BẢN </h3>
        <ul class="tabs">
            <li><a href="#tab1">Tất cả</a></li>
<!--            <li ><a href="#tab2"><span class="glyphicon glyphicon-plus"  >Thêm</span></a></li>-->
        </ul>

    </header>
    <div class="tab_container"  style="overflow-y:scroll; height:400px;width: 100%;">
        <div id="tab1" class="tab_content">
            <table class="table demo default  footable tablesorter table-hover" cellspacing="0" > 
                <thead> 
                    <tr> 
                        <th data-type="numeric" data-sort-initial="true" >STT</th> 
                        <th  >Mã NXB</th> 
                        <th >Tên NXB </th> 
<!--                        <th >Địa chỉ </th>                        -->
                        <th data-sort-ignore="true">Hành động</th> 
                    </tr> 
                </thead> 
                <tbody id="category"> 
                    <?php
                    $i = 0;

                    foreach ($nxbs as $value) {

                        $i+=1;
                        echo "<tr id='row" . $value->MANXB . "'>";
                        echo '<td>' . $i . '</td>';
                        echo '<td class="td_manxb">' . $value->MANXB . '</td>';
                        echo '<td class="td_tennxb">' . $value->TENNXB . '</td>';
//                        echo '<td class="td_ngsinh">' . ($value->DIACHI) . '</td>';

                        echo "<td><a class='edit-category' href='" . $value->MANXB . "'><input type='image' src='" . base_url() . "public/admin/images/icn_edit.png' title='Chỉnh sửa thể loại sách '></a>
					<a class='delete-cate' href='" . $value->MANXB . "'><input type='image' src='" . base_url() . "public/admin/images/icn_trash.png'title='thể loại sách'></a>
					<a class='add-cate'><input type='image' src='" . base_url() . "public/admin/images/icn_add_user.png' title='Thêm loại sách mới'></a></td>";
                        echo '</tr>';
                    }
                    ?>
                </tbody> 
            </table>
        </div><!-- end of #tab1 -->
<!--        <div id="tab2" class="tab_content">
            <form class="form-horizontal" role="form">
                <div class="form-group">
                    <label for="inputEmail3" class="col-sm-2 control-label">Tên nhà xuất bản</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control tennxb"  id="tennxb" name="tennxb" placeholder="Tên nhà xuất bản">
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputPassword3" class="col-sm-2 control-label">Password</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control"  id="inputPassword3" placeholder="Password">
                    </div>
                </div>
                <div class="form-group">
                    <div class=" col-sm-12">
                        <?php echo form_textarea(array('name' => 'diachi', 'id' => 'diachi', 'class' => "ckeditor diachi")); ?>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="button" id="add_form" class="btn btn-default">Thêm mới</button>
                    </div>
                </div>
            </form>
        </div> end of #tab Thêm -->






    </div><!-- end of .tab_container -->

</article><!-- end of content manager article -->	
<form id="edit_form" class="contact_form" method="post" name="contact_form">
</form>	
<form id="add_form1" class="contact_form" method="post" name="contact_form">
</form>


<!-- Button trigger modal -->

<!-- Modal add -->
<div class="modal fade" id="myModal-add" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Thêm mới nhà xuất bản</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Tên nhà xuất bản</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control tennxb"  id="tennxb-add" name="tennxb" placeholder="Tên nhà xuất bản">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class=" col-sm-12">
                            <?php echo form_textarea(array('name' => 'diachi', 'id' => 'diachi-add', 'class' => "ckeditor diachi", 'placeholder' => 'Thông tin về nhà xuất bản')); ?>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" id="btn-add" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>
<!-- Modal edit -->
<div class="modal fade" id="myModal-edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Chỉnh sửa nhà xuất bản</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" role="form">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Mã nhà xuất bản</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control tennxb"  id="manxb-edit" name="tennxb" placeholder="Tên nhà xuất bản">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">Tên nhà xuất bản</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control tennxb"  id="tennxb-edit" name="tennxb" placeholder="Tên nhà xuất bản">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class=" col-sm-12">
                            <?php echo form_textarea(array('name' => 'diachi', 'id' => 'diachi-edit', 'class' => "ckeditor diachi", 'placeholder' => 'Thông tin về nhà xuất bản')); ?>
                        </div>
                    </div>

                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" id="btn-save" class="btn btn-primary">Lưu</button>
            </div>
        </div>
    </div>
</div>

<!-- end modal edit -->

<script>
    $(document).ready(function() {
     $('.add-cate').click(function(e) {
            e.preventDefault();
             $("#myModal-add").modal();
            
        });
        $("#btn-add").click(function(e) {
            e.preventDefault();            
            var tennxb = $("#tennxb-add").val();
            var diachi = CKEDITOR.instances['diachi-add'].getData();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/nxb_add',
                data: {
                    tennxb: tennxb,
                    diachi: diachi
                },
                success: function(data) {
                    // $('#category').append(data);
                   
                    $('#myModal-add').modal('hide');

                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        });
        $("#btn-save").click(function(e) {
            e.preventDefault();
            var tennxb = $("#myModal-edit #tennxb-edit").val();
            var diachi = CKEDITOR.instances['diachi-edit'].getData();
            var manxb = $("#myModal-edit #manxb-edit").val();          
            //alert(manxb);
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/NXB_upd',
                data: {
                    manxb: manxb,
                    tennxb: tennxb,
                    diachi: diachi
                },
                success: function(data) {                   
                    // $('#category').append(data);
                    $('#myModal-edit').modal('hide');                  
                     $('#row' + manxb).find(".td_manxb").text(data.manxb);                                       
                    $('#row' + manxb).find(".td_tennxb").text(data.tennxb);

                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                },dataType: "json"
            });
        });
        $('.edit-category').click(function(e) {
            e.preventDefault();            
            var manxb = $(this).attr("href");
        
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/GetNXB',
                data: {
                    manxb: manxb
                },
                success: function(data) {
                  
                    CKEDITOR.instances["diachi-edit"].setData(data.diachi);
                    $("#myModal-edit #manxb-edit").val(manxb);
                    $("#myModal-edit").modal();
                    $("#myModal-edit #tennxb-edit").val(data.tennxb);
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }, dataType: "json"
            });
        });
        $('#edit_form').submit(function(e) {
            e.preventDefault();
            var matl = $('#cateid').val();
            var tentl = $('#catename').val();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/updatetheloai',
                data: {
                    matl: matl,
                    tentl: tentl
                },
                success: function(data) {

                    $('#row' + matl).find(".td_manxb").text($(data).filter('#matl').text());
                    $('#row' + matl).find(".td_tennxb").text($(data).filter('#tentl').text());
                    $('#edit_form').toggle("slow");
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        });
        $('.delete-cate').click(function(e) {
            e.preventDefault();
            var manxb = $(this).attr("href");
            alert(manxb);
            if (confirm('Bạn có muốn xóa nhà xuất bản này không ?')) {
                $.ajax({
                    type: "POST",
                    url: globalurl + 'index.php/admin/deteleNXB',
                    data: {
                        manxb: manxb
                    },
                    success: function(data) {                      
                        $('#row' + manxb).remove();
                    }, error: function(xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
            }

        });
       
    
        $('#add_form').submit(function(e) {
            e.preventDefault();
            alert('d');
            var tentl = $('#catename').val();
            $.ajax({
                type: "POST",
                url: globalurl + 'index.php/admin/addnewtheloai',
                data: {
                    tentl: tentl
                },
                success: function(data) {
                    // $('#category').append(data);
                    alert(data);
                    $('#add_form').toggle("slow");
                }, error: function(xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
        });
    });

</script>
